import os
import subprocess

# 启动Mosquitto MQTT服务器
def start_mosquitto_server():
    command = "mosquitto"
    process = subprocess.Popen(command, shell=True)
    process.wait()

# 停止Mosquitto MQTT服务器
def stop_mosquitto_server():
    command = "pkill mosquitto"
    os.system(command)

# 主函数
def main():
    try:
        # 启动MQTT服务器
        start_mosquitto_server()

        # 保持主程序运行
        while True:
            pass
    except KeyboardInterrupt:
        # 捕捉键盘中断信号
        pass

        # 停止MQTT服务器
        stop_mosquitto_server()

# 运行主程序
if __name__ == "__main__":
    main()
